package com.qiantu.phone.ui.dialog;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.qiantu.api.entity.ControllerBrandProtocolBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;
import com.lxj.xpopup.core.BottomPopupView;

import java.util.List;

public class BottomKGXDeviceAddressDialog extends BottomPopupView {

    private DialogClickListener mDialogClickListener;
    private WheelView wv_first;
    private ArrayWheelAdapter mFirstArrayWheelAdapter;
    private List<String> counts;
    private String selectCount;

    public BottomKGXDeviceAddressDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_kg_device_485address;
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
        wv_first = findViewById(R.id.options1);
        View btnSave = findViewById(R.id.btn_save);
        btnSave.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mDialogClickListener != null){
                    mDialogClickListener.onConfirmClick(selectCount);
                }
            }
        });
        setStyle();
        initWheelView();
    }

    public void setCounts(List<String> counts){
        this.counts = counts;
        initWheelView();  // 更新 WheelView 的内容
    }

    public void setSelected(String count){
        this.selectCount = count;
        setCurrentItem();
    }

    private void setStyle() {
        wv_first.setGravity(Gravity.CENTER);
        wv_first.setCyclic(false);
        wv_first.setAlphaGradient(true);
        wv_first.setLabelOffset(5);
        wv_first.setItemsVisibleCount(5);
        wv_first.setDividerColor(0x00000000);
        wv_first.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_first.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));

    }

    private void initWheelView(){
        if(selectCount == null){
            return;
        }
        mFirstArrayWheelAdapter  = new ArrayWheelAdapter(counts);
        wv_first.setAdapter(mFirstArrayWheelAdapter);
        setCurrentItem();

        wv_first.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                selectCount = counts.get(index);
            }
        });

    }

    private void setCurrentItem(){
        if(wv_first == null){
            return;
        }
        int selectCountIndex = 0;
        for(int i=0; i<counts.size(); i++){
            if (counts.get(i).equals(selectCount)) {
                selectCountIndex = i;
                break;
            }
        }
        wv_first.setCurrentItem(selectCountIndex);
    }


    // 设置最大宽度，看需要而定
    @Override
    protected int getMaxWidth() {
        return super.getMaxWidth();
    }

    // 设置最大高度，看需要而定
    @Override
    protected int getMaxHeight() {
        return super.getMaxHeight();
    }

    /**
     * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
     *
     * @return
     */
    protected int getPopupWidth() {
        return 0;
    }

    /**
     * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
     *
     * @return
     */
    protected int getPopupHeight() {
        return 0;
    }

    public void setDialogClickListener(@Nullable DialogClickListener listener) {
        mDialogClickListener = listener;
    }

    public interface DialogClickListener {
        void onConfirmClick(String  count);
    }
}
